#!/usr/bin/env python
# encoding: utf-8

# from werkzeug.security import generate_password_hash
from werkzeug.security import check_password_hash
from flask_login import UserMixin
import uuid

# define profile.json constant, the file is used to
# save user name and password_hash
USER_LISTS={"shzu": ("TJUBIGDATA", 'fe98ac38-47d5-4dfc-accb-9ebfb0526639'),"root": ("root", '7CFF709A-2B54-6354-9D53-8787366F6B1C')}


class User(UserMixin):
    def __init__(self, username):
        self.username = username
        self.id = self.get_id()

    @property
    def password(self):
        raise AttributeError('password is not a readable attribute')

    @password.setter
    def password(self, password):
        """save user name, id and password hash to json file"""
        # self.password_hash = generate_password_hash(password)
        USER_LISTS[self.username] = [self.password, self.id]

    def verify_password(self, password):
        # password_hash = self.get_password_hash()
        if password is None:
            return False
        return self.username in USER_LISTS and USER_LISTS[self.username][0] == password

    def get_password_hash(self):
        """try to get password hash from file.

        :return password_hash: if the there is corresponding user in
                the file, return password hash.
                None: if there is no corresponding user, return None.
        """
        pass
        # try:
        #     with open(PROFILE_FILE) as f:
        #         user_profiles = json.load(f)
        #         user_info = user_profiles.get(self.username, None)
        #         if user_info is not None:
        #             return user_info[0]
        # except IOError:
        #     return None
        # except ValueError:
        #     return None
        # return None

    def get_id(self):
        """get user id from profile file, if not exist, it will
        generate a uuid for the user.
        """
        if self.username is not None:
            if self.username in USER_LISTS:
                return USER_LISTS[self.username][1]
        return unicode(uuid.uuid4())

    @staticmethod
    def get(user_id):
        """try to return user_id corresponding User object.
        This method is used by load_user callback function
        """
        if not user_id:
            return None
        for user_name, profile in USER_LISTS.items():
            if profile[1] == user_id:
                return User(user_name)
